package cn.blnp.net.flowable.task.ext.web;

import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.TaskQuery;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <h3>web service 演示案例作废</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/5/16 17:07
 */
@Slf4j
@SpringBootTest
public class WebServiceTaskTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;

    /**
     *
     **/
    @Test
    public void test() {

    }


    /**
     *  整合无效果，查询相关资料较少 暂无完整解决方案
     **/
    @Test
    public void testExample() {
        Deployment deploy = repositoryService.createDeployment()
                .name("接收任务演示案例")
                .addClasspathResource("flow/webServiceTaskProcess.bpmn20.xml")
                .deploy();
        log.info("部署流程定义：{}", deploy.getId());
        ProcessDefinition procDef = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义编号：{}", procDef.getId());
        //设置流程变量
        Map<String, Object> varMap = MapUtil.builder(new HashMap<String, Object>())
                .put("userName", "诗雨花魂")
                .put("userMail", "3102384158@qq.com")
                .put("myMobileCode", "13693511055")
                .build();
        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(procDef.getId(), varMap);
        //查询并打印流程变量
        List<HistoricVariableInstance> hisVariables = historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(procInst.getId()).list();
        hisVariables.stream().forEach(hisVariable -> log.info("流程变量名：{}，变量值：{}",
                hisVariable.getVariableName(), hisVariable.getValue()));
    }
}
